package com.AI.SDK.Client;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.AI.SDK.model.ChartInfo;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.broadscope.bailian.sdk.AccessTokenClient;
import com.aliyun.broadscope.bailian.sdk.ApplicationClient;
import com.aliyun.broadscope.bailian.sdk.models.BaiLianConfig;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsRequest;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;

import java.util.HashMap;


@Data
@Slf4j
public class AIClient {
    private String accessKey;
    private String secretKey;

    public AIClient(String accessKey,String secretKey){
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }


    /**
     * 调用AI服务方法，调用消息生产者方法
     * @param csvData csv参数
     */
    public String invokeAI(String csvData,String chartName,String goal,String chartType){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("Content-Type","application/json");
//        hashMap.put("body", csvData);
        //todo 获取系统当前时间戳放到请求头中
        long currentTimeMillis = System.currentTimeMillis() / 1000;
        hashMap.put("timestamp",String.valueOf(currentTimeMillis));
        //todo 获取ak与签名放到请求头中
        hashMap.put("accessKey",this.accessKey);
        hashMap.put("sing",this.secretKey);
        ChartInfo chartInfo = new ChartInfo();
        chartInfo.setCsvData(csvData);
        chartInfo.setChartName(chartName);
        chartInfo.setGoal(goal);
        chartInfo.setChartType(chartType);
        String params = JSONUtil.toJsonStr(chartInfo);
        //调用接口返回结果
        HttpResponse httpResponse = HttpRequest.post("http://127.0.0.1:9898/ai/invokerBI")
                .addHeaders(hashMap)
                .body(params)
                .execute();
        log.info("SDK调用结果:{}",httpResponse);
        return httpResponse.body();
    }

    public static void main(String[] args) throws JSONException {
        String accessKeyId = "LTAI5tEqhMw9VP83Rm9xb54d";
        String accessKeySecret = "vbgDlknODJSy4wggHvkTEGDzKo33TG";
        String agentKey = "30161e06a9544fe9b22b3261b86a3e60_p_efm";

        //拿到token
        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();

        //根据token获取百炼模型的Config
        BaiLianConfig config = new BaiLianConfig()
                .setApiKey(token);

        String appId = "4d7710dd5b894a05b7ec79e81b4003d9";
        String prompt = "北京有哪些景点吗";
        // sessionID 就是用来做多轮对话的，sessionID相同的情况下保存对话上下文
        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setPrompt(prompt)
                .setSessionId("111111111");
        ApplicationClient client = new ApplicationClient(config);
        CompletionsResponse response = client.completions(request);
        //历史
//        List<CompletionsRequest.ChatQaPair> history = new ArrayList<>();
//        CompletionsRequest.ChatQaPair chatQaPair = new CompletionsRequest.ChatQaPair("我想去北京", "北京的天气很不错");
//        CompletionsRequest.ChatQaPair chatQaPair2 = new CompletionsRequest.ChatQaPair("北京有哪些景点", "北京有故宫、长城等");
//        history.add(chatQaPair);
//        history.add(chatQaPair2);
//        request.setHistory(history);
        //透传
        JSONObject entries = new JSONObject();
        entries.put("我想去北京","北京的天气很不错");
        entries.put("北京有哪些景点","北京有故宫、长城等");
        request.setBizParams(entries);
        System.out.println(response.getData());
    }

}
